<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 入库单ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

//加载订单商品生产表查询类
quanli_include('module/base/bill/billGoodsProduct.class.php');
class ajax_voucherGoods {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->searchVoucherGoods();      //查询入库单列表
                    break;

                case 2:
                    $this->searchBillGoodsInfo();      //查询入库单商品信息
                    break;

                case 3:
                    $this->executeChange();      //执行与头查询条件不匹配的订单入库
                    break;

                default:
                    $this->toReturn('参数错误');
                    break;
            }
        } else {
            $this->toReturn('参数错误');
        }
    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn($message, $code = 400, $data = '') {

        die(json_encode(array('code' => $code, 'message' => $message, 'data' => $data)));
    }







    /**
     * 查询入库单列表
     */
    private function searchVoucherGoods() {

        global $G_PGLOB;

        //实例化入库单类
        $billGoodsProduct = new billGoodsProduct();

        //调用分组查询订单商品生产表方法
        $billGoods_array = $billGoodsProduct->searchbillGoodsProductGroup($_REQUEST['formValues']);

        if ($billGoods_array) {
            returnJsonData('ok','200',$billGoods_array);
        }else{
            returnJsonData('暂无对应入库单数据','400');
        }

    }


    /**
     * 查询入库单列表
     */
    private function searchBillGoodsInfo() {


        global $G_PGLOB;

        if(!isset($_REQUEST['bill_goods_no']) || empty($_REQUEST['bill_goods_no'])){
            returnJsonData('缺少参数','400');
        }

        if(!isset($_REQUEST['formValues']['send_date']) || empty($_REQUEST['formValues']['send_date'])){
            returnJsonData('缺少参数','400');
        }

        //实例化订单商品生产表查询类
        $billGoodsProduct = new billGoodsProduct();

        //订单商品流水号参数
        $arg_array = array('bill_goods_no'=>$_REQUEST['bill_goods_no']);

        //调用查询订单商品生产表方法
        $billGoods_array = $billGoodsProduct->searchBillGoodsInfo($arg_array);

        if(!$billGoods_array){
            returnJsonData('暂无该订单商品流水号[' .$_REQUEST['bill_goods_no']. ']入库单数据','400');
        }

        //判断是否入库 1为入库
        if($billGoods_array[0]['is_voucher'] == 1){
            returnJsonData('该订单商品流水号[' .$_REQUEST['bill_goods_no']. ']已入库，无需入库操作','400');
        }

        //对应订单商品流水号相关信息
        $bill_goods_product = new entity('bill_goods_product');
        $bill_goods_product->bill_goods_no = $_REQUEST['bill_goods_no'];
        if (!$G_PGLOB->DBCA->fetch($bill_goods_product)){
            returnJsonData('暂无对应入库单数据','400');
        }

        //将头部查询条件与当前订单生产流水号匹配
        $data = $_REQUEST['formValues'];

        $res_eq = 0;//标记头部查询条件与当前订单生产流水号是否匹配 匹配 标记为0

        //配送日期
        if($bill_goods_product->send_date == $data['send_date']){
            $res_eq +=0;
        }else{
            $res_eq +=1;
        }

        //配送开始时间
        if(isset($_REQUEST['send_start_time']) && !empty($_REQUEST['send_start_time'])){
            if($bill_goods_product->send_start_time == $data['send_start_time']){
                $res_eq +=0;
            }else{
                $res_eq +=1;
            }
        }

        //配送结束时间
        if(isset($_REQUEST['send_end_time']) && !empty($_REQUEST['send_end_time'])){
            if($bill_goods_product->send_end_time == $data['send_end_time']){
                $res_eq +=0;
            }else{
                $res_eq +=1;
            }
        }

        //城市id
        if(isset($_REQUEST['cityID']) && !empty($_REQUEST['cityID'])){
            if($bill_goods_product->cityID == $data['cityID']){
                $res_eq +=0;
            }else{
                $res_eq +=1;
            }
        }

        //配送站id
         if(isset($_REQUEST['distribution_id']) && !empty($_REQUEST['distribution_id'])){
             if($bill_goods_product->distribution_id == $data['distribution_id']){
                 $res_eq +=0;
             }else{
                 $res_eq +=1;
             }
         }

         if($res_eq == 0){ //与头部查询条件匹配
             //入库
             $bill_goods_product = new entity('bill_goods_product');
             $bill_goods_product->bill_goods_no = $_REQUEST['bill_goods_no'];
             if (!$G_PGLOB->DBCA->fetch($bill_goods_product)){
                 returnJsonData('暂无对应入库单数据','400');
             }
             $bill_goods_product->is_voucher = 1; //入库状态
             $bill_goods_product->voucher_operator = $G_PGLOB->currentUserID;
             $bill_goods_product->voucher_time = date("Y-m-d H:i;s");
             $G_PGLOB->DBCA->save($bill_goods_product);
             returnJsonData('ok','200',$billGoods_array);
         }else{ //不匹配
             returnJsonData('ok','201',$billGoods_array);
         }

    }

    /*
     *
     * 执行与头查询条件不匹配的订单入库
     */
    public function executeChange(){
        global $G_PGLOB;

        if(!isset($_REQUEST['bill_goods_no']) || empty($_REQUEST['bill_goods_no'])){
            returnJsonData('缺少参数','400');
        }

        //入库
        $bill_goods_product = new entity('bill_goods_product');
        $bill_goods_product->bill_goods_no = $_REQUEST['bill_goods_no'];
        if (!$G_PGLOB->DBCA->fetch($bill_goods_product)){
            returnJsonData('暂无对应入库单数据','400');
        }
        $bill_goods_product->is_voucher = 1; //入库状态
        $bill_goods_product->voucher_operator = $G_PGLOB->currentUserID;
        $bill_goods_product->voucher_time = date("Y-m-d H:i;s");
        $G_PGLOB->DBCA->save($bill_goods_product);
        returnJsonData('ok','200',$_REQUEST['bill_goods_no']);
    }
}
